package org.jackysoft.webapp;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;

import javax.annotation.PostConstruct;
import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jackysoft.entity.User;
import org.jackysoft.ireporter.IReporterTool;
import org.jackysoft.util.SearchUtils;
import org.jackysoft.util.SessionKey;
import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.web.filter.GenericFilterBean;

import com.google.common.collect.Maps;

public class ZZZSessionConfigFilter extends GenericFilterBean {

	private static final Log log = LogFactory
			.getLog(ZZZSessionConfigFilter.class);
	private static final String APP_PROPERTIES = "classpath:commons.properties";
	private static String sbase = null;
	private static String logouturl = null;
	public static Map<String,Boolean> filtered = Maps.newConcurrentMap();

	@Override
	public void destroy() {
	}

	@PostConstruct
	public void init() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain fc) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) arg0;
		HttpSession session = request.getSession();	
		String sid = session.getId();
		if(!ZZZSessionConfigFilter.filtered.containsKey(sid)){
			ZZZSessionConfigFilter.filtered.put(sid, false);
		}
		Boolean rst  = ZZZSessionConfigFilter.filtered.get(sid);		
		if (!rst) {

			initSource(arg0.getServletContext());
			
			log.info("Application Context Variable is configed for URL : " + request.getRequestURL());
			ClassRelativeResourceLoader cl = new ClassRelativeResourceLoader(
					this.getClass());
			InputStream ins = cl.getResource(APP_PROPERTIES).getInputStream();
			Properties sprops = new Properties();
			sprops.load(ins);
			sbase = sprops.getProperty("static.site");			
			logouturl = sprops.getProperty("cas.logouturl");			
			String newsHost = sprops.getProperty("newsInfoHost");
			String path = request.getContextPath();
			String basePath = request.getScheme() + "://"
					+ request.getServerName() + ":" + request.getServerPort()
					+ path + "/";

			String httpBase = arg0.getServletContext().getRealPath("");
			session.setAttribute(SessionKey.HTTP_BASE_PATH, basePath);

			//TODO 获取当前登陆用户
			User user = null;
			session.setAttribute("loginUser", user);

			Properties props = System.getProperties();
			props.setProperty("newsHost", newsHost);
			props.setProperty("appBase", httpBase);
			props.setProperty("sbase", sbase);
			props.setProperty("base", basePath);
			props.setProperty("logouturl", logouturl);
			props.setProperty("contextPath", path);
			props.setProperty("webenv", "true");
			session.setAttribute("newsHost", newsHost);
			session.setAttribute("base", basePath);			
			session.setAttribute("sbase", sbase);			
			session.setAttribute("logouturl", logouturl);			
			session.setAttribute("appBase", httpBase);			
			session.setAttribute("contextPath", path);
			ZZZSessionConfigFilter.filtered.put(sid, true);
			log.info("Application Context Variable Set");
		}else{
			log.info("Application Context has set ");
		}
		fc.doFilter(arg0, arg1);

	}

	
	public void initSource(ServletContext sc) throws ServletException {
				
		File f = new File(sc.getRealPath("/jasper/"));
		if (f.exists()) {
			log.info("start to compile jasper reporter");
			IReporterTool.compileReport(f);
			SearchUtils.indexAll(sc);
			log.info("jasper compiled -------[OK]");
		}

	}

}
